import { defineStore } from "pinia";
import { ref, computed } from "vue";
import { codeWeather, getCity, getCode, weatherForecast, inputTip } from "../api";
import { color } from "echarts";


const useStore = defineStore('city-list', () => {

    const addCityName = ref('')
    const cityTip = ref('')
    const cityInfo = ref({})
    const cityCode = ref({})
    const cityWeather = ref({})
    const cityForcast = ref({})
    const isSave = ref(false)
    const isInputText = ref(false)
    const isInputSearch = ref(false)

    // api相关
    const weather = ref()
    const temperature = ref()
    const wind = ref()
    const windLever = ref()
    const date = ref([])
    const week = ref([])
    const dayWeather = ref([])
    const cloudPower = ref([])
    const dayTemp = ref([])
    const nightTemp = ref([])


    // 画图方法
    const renderChart = (payload, time, textTime, color) => {
        payload.value = {
            xAxis: {
                type: 'category',
                show: false
            },
            yAxis: {
                type: 'value',
                show: false
            },
            grid: {
                top: 100,
                left: 0,
                right: 0,
                bottom: 50,
            },
            series: [
                {
                    data: time,
                    type: 'line',
                    itemStyle: {
                        color,
                    },
                    label: {
                        show: true,
                        color: '#fff',
                        formatter(params) {
                            return textTime + params.data + '°C'
                        }
                    }
                }
            ]
        }
    }

    // 异步方法,得到城市
    async function ipGetcity() {
        cityInfo.value = await getCity('7633c039e776357d868a4d0f4c7f0e9c')
    }

    //

    // 异步方法,得到城市adcode
    async function getAdcode() {
        await ipGetcity()
        cityCode.value = await getCode(cityInfo.value.city, '7633c039e776357d868a4d0f4c7f0e9c')
        cityCode.value = cityCode.value.geocodes[0].adcode
    }



    // 异步方法,得到天气情况
    async function codeGetweather() {
        await getAdcode()
        cityWeather.value = await codeWeather(cityCode.value, '7633c039e776357d868a4d0f4c7f0e9c');
        weather.value = cityWeather.value.lives[0].weather
        temperature.value = cityWeather.value.lives[0].temperature
        wind.value = cityWeather.value.lives[0].winddirection
        windLever.value = cityWeather.value.lives[0].windpower
    }

    // 异步方法,得到预测天气
    async function forecastWeather() {
        await getAdcode()
        cityForcast.value = await weatherForecast(cityCode.value, '7633c039e776357d868a4d0f4c7f0e9c')
        // 日期
        date.value = cityForcast.value.forecasts[0].casts.map((item) => {
            return item.date.slice(5, 10)
        })
        // 星期
        week.value = cityForcast.value.forecasts[0].casts.map((item) => {
            return item.week
        })
        week.value = week.value.map(numberToChinese)

        // 全天气候
        dayWeather.value = cityForcast.value.forecasts[0].casts.map((item) => {
            return item.dayweather
        })

        //风力
        cloudPower.value = cityForcast.value.forecasts[0].casts.map((item) => {
            return item.nightpower
        })

        // 白天温度
        dayTemp.value = cityForcast.value.forecasts[0].casts.map((item) => {
            return item.daytemp
        })

        // 夜晚温度
        nightTemp.value = cityForcast.value.forecasts[0].casts.map((item) => {
            return item.nighttemp
        })

    }
    //

    // 转换大小写函数
    function numberToChinese(num) {
        switch (num) {
            case "1": return "一";
            case "2": return "二";
            case "3": return "三";
            case "4": return "四";
            case "5": return "五";
            case "6": return "六";
            case "7": return "日";
            default: return num;
        }
    }

    // 主方法
    async function mainFunction() {
        await ipGetcity(); // 等待ipGetcity完成
        await getAdcode()
        await codeGetweather(); // 再调用getAdcode
        await forecastWeather()
    }

    mainFunction(); // 调用主函数

    // 跳转页面 查看点击的城市
    const searchCity = () => {
        console.log('这是新窗口')
    }

    // 点击添加方法
    const addCity = (payload) => {
        isSave.value = true
        addCityName.value = payload
    }

    //删除城市
    const deleteCity = () => {
        isSave.value = false
    }

    return {
        addCityName,
        cityTip,
        cityWeather,
        cityCode,
        cityInfo,
        cityForcast,
        isSave,
        isInputText,
        isInputSearch,
        renderChart,
        ipGetcity,
        getAdcode,
        codeGetweather,
        mainFunction,
        forecastWeather,
        searchCity,
        addCity,
        deleteCity,

        // api相关
        weather,
        temperature,
        wind,
        windLever,
        date,
        week,
        dayWeather,
        cloudPower,
        dayTemp,
        nightTemp,


    }

})

export { useStore }